import { getGiftRankList, diamondRecharge } from '../../api/recharge/recharge';
import { wxPay } from '../../api/pay';
import { timeoutTask, showWxToast } from '../../utils/index';

const app = getApp();
let requestLock = false;
Page({
    data: {
        userId: '',
        openId: '',
        money: 0,
        remainMoney: 0,
        hbyRemainMoney: 0,
        nickname: '',
        headImg: '',
        gender: '1',
        rechargeList: [],
        giftRankList: [],
        myRank: 0,
        myRankHq: 0,
        miaobiRechargePopupVisible: false,
        miaobiRechargePopupAni: false,
        deskNum: '',
        editUserInfoPrice: '0',
        currentStatus: '',
    },
    onLoad: function (options) {
        this.setData({
            userId: options.userId,
            openId: options.openId,
            remainMoney: options.money,
            hbyRemainMoney: options.hbyMoney,
            rechargeList: options.rechargeListStr.split(','),
            nickname: options.name,
            headImg: options.headImg,
            gender: options.gender,
            editUserInfoPrice: options.editUserInfoPrice,
            deskNum: options.deskNum,
            currentStatus: options.currentStatus,
        });
        this.getGiftRankList();
    },
    onShow: function () {
    },
    onUnload: function () {
    },
    handleRecharge: function () {
        if (!this.data.money) {
            showWxToast('请输入或者选择充值金额!');
            return;
        }
        if (this.data.money === '0') {
            showWxToast('充值金额不能为0!');
            return;
        }
        this.recharge();
    },
    recharge: function (e) {
        this.setData({
            money: e.currentTarget.dataset.desc,
        });
        if (requestLock) {
            return;
        }
        requestLock = true;
        diamondRecharge({
            total: this.data.money,
            userId: this.data.userId,
            openId: this.data.openId,
        }).then((res) => {
            console.log(res);
            return wxPay(res);
        }).then(() => {
            // 前端处理用户余额增加
            const tmpRemainMoney = (parseFloat(this.data.remainMoney) + parseFloat(this.data.money)).toFixed(2);
            this.setData({
                remainMoney: tmpRemainMoney,
            });
            showWxToast('充值成功!');
            requestLock = false;
            app.globalData.isH5NeedRefresh = true;
        }).catch((err) => {
            console.log(err);
            showWxToast('充值失败!');
            requestLock = false;
        });
    },
    getGiftRankList: function () {
        getGiftRankList().then((res) => {
            console.log(res);
            const tmpList = res.data.list;
            const tmpLen = tmpList.length;
            for (let i = 0; i < tmpLen; i += 1) {
                tmpList[i].num = (i + 1) < 10 ? '0' + (i + 1) : i + 1;
                tmpList[i].wx_name = tmpList[i].wx_name.length <= 6 ? tmpList[i].wx_name : tmpList[i].wx_name.substr(0, 6) + '...';
                if (tmpList[i].wx_name === this.data.nickname && tmpList[i].avator === this.data.headImg) {
                    this.setData({
                        myRank: (i + 1),
                        myRankHq: tmpList[i].gift_price,
                    });
                }
            }
            this.setData({
                giftRankList: tmpList,
            });
            console.log(this.data.giftRankList);
        }).catch((err) => {
            console.log(err);
        });
    },
    toAgreement: function () {
        wx.navigateTo({
            url: '/pages/agreement/agreement',
        });
    },
    hideMiaobiRechargePopup: function () {
        this.setData({
            miaobiRechargePopupAni: false,
        });
        timeoutTask(() => {
            this.setData({
                miaobiRechargePopupVisible: false,
            });
        }, 300);
    },
    // 跳转到反馈页面
    toFeedback: function () {
        wx.navigateTo({
            url: '/pages/feedback/feedback?userId=' + this.properties.userId,
        });
    },
    toHbDetail: function () {
        wx.navigateTo({
            url: '/pages/userCenter/hbDetail/hbDetail?hbyRemainMoney='+ this.data.hbyRemainMoney,
        });
    },
    toWithdraw: function () {
        wx.navigateTo({
            url: '/pages/userCenter/withdraw/withdraw?hbyRemainMoney='+ this.data.hbyRemainMoney,
        });
    },
    toEditInfo: function () {
        console.log(`/pages/userCenter/editInfo/editInfo?nickname=${this.data.nickname}&headImg=${this.data.headImg}&currentStatus=${this.data.currentStatus}&editUserInfoPrice=${this.data.editUserInfoPrice}&deskNum=${this.data.deskNum}`);
        wx.navigateTo({
            url: `/pages/userCenter/editInfo/editInfo?nickname=${this.data.nickname}&headImg=${this.data.headImg}&currentStatus=${this.data.currentStatus}&editUserInfoPrice=${this.data.editUserInfoPrice}&deskNum=${this.data.deskNum}`,
        });
    },
    openRecharge: function () {
        this.setData({
            miaobiRechargePopupVisible: true,
        });
        timeoutTask(() => {
            this.setData({
                miaobiRechargePopupAni: true,
            });
        }, 300);
    },

});